
from app.web.casesuit.get_check import get_code
import requests
from app.web.conf.config import  *
class GetToken():
    requests.packages.urllib3.disable_warnings()  # Python的第三方库requests提示警告InsecureRequestWarning的问题
    def get_uni_code(self):  # 获取登录页返回的图片unique_code
        url = base_url_cloud + '/v3/user/picture-code?token='

        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}

        response=requests.get(url=url,headers=headers, verify=False)
        print(response)
        res = response.json()['data']['unique_code']
        print("获取登录页返回图片请求地址：%s"%url)
        # print(res)
        return res

    # def get_check_code(self):#获取验证码
    #     code=get_code(self.get_uni_code())#根据上面接口获得的unique_code换取redis里的code
    #     return code

    def login_token(self,tax_code,password):#登录获取token
        url = base_url_cloud + '/v3/user/account-login?token='
        print("获取token接口url:%s"%url)
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}
        request_data={
            "tax_code":tax_code,
            "password":password,
            "picture_code":"73bd",
            "unique_code":"picture_1573223473356157322347335691"
        }
        unique_code=self.get_uni_code()
        print(unique_code)
        request_data['picture_code']=get_code(unique_code)
        request_data['unique_code'] = unique_code
        print("请求数据：%s"%request_data)
        response=requests.post(url=url,json=request_data,headers=headers, verify=False)
        # res=response.json()
        # res = response.json()['data']['token']
        try:
            res = response.json()['data']['token']
            return res
        except:
            print("登录不了")
        # print("请求地址：%s"%url)
        # print(res)


if  __name__=="__main__":
    G=GetToken()
    G.get_uni_code()
    # a=G.get_check_code()
    # print(a)
    tax_code= "91140100751525588X"
    password="test123456"
    token=G.login_token(tax_code,password)
    print(token)